﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;

public partial class ChangeRequestEN : System.Web.UI.Page
{

    public Persons mobjPersons
    {
        get
        {
            if (ViewState["mobjPersons"] == null)
                ViewState["mobjPersons"] = System.Guid.NewGuid().ToString();
            return Session[ViewState["mobjPersons"].ToString()] as Persons;

        }
        set
        {
            if (ViewState["mobjPersons"] == null)
                ViewState["mobjPersons"] = System.Guid.NewGuid().ToString();
            Session[ViewState["mobjPersons"].ToString()] = value;
        }
    }



    public List<Ticket> lstTicketGrid
    {
        get
        {
            if (ViewState["lstTicketGrid"] == null)
                ViewState["lstTicketGrid"] = System.Guid.NewGuid().ToString();
            return Session[ViewState["lstTicketGrid"].ToString()] as List<Ticket>;

        }
        set
        {
            if (ViewState["lstTicketGrid"] == null)
                ViewState["lstTicketGrid"] = System.Guid.NewGuid().ToString();
            Session[ViewState["lstTicketGrid"].ToString()] = value;
        }
    }


    public bool? bHistory
    {
        get
        {
            if (ViewState["bHistory"] == null)
                ViewState["bHistory"] = System.Guid.NewGuid().ToString();
            return Session[ViewState["bHistory"].ToString()] as bool?;
        }
        set
        {
            if (ViewState["bHistory"] == null)
                ViewState["bHistory"] = System.Guid.NewGuid().ToString();
            Session[ViewState["bHistory"].ToString()] = value ;

        }
    }



    /// <summary>
    /// this function will bind the grid with the returned list form function search
    /// </summary>
    /// <param name="binder">the text to search for</param>
    protected void bindGrid(string binder)
    {
        if (lstTicketGrid == null)
            lstTicketGrid = new List<Ticket>();
        lstTicketGrid = search(binder);

        grdUsers.DataSource = lstTicketGrid;
        grdUsers.DataBind();
    }




    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request["history"] == null)
            {
                bHistory = false;
            }
            else
            {
                bHistory = true;
            }

            checkSecurityAndCustomizePage();

            bindGrid("");

            
        }
    }


    /// <summary>
    /// this function will check the security and redirect the page if there is security error
    /// </summary>
    private void checkSecurityAndCustomizePage()
    {
        mobjPersons = GlobalVars.getCurrentPerson();

        if (!mobjPersons.lstAllRulesIDs.Contains((long)RulesEnum.Ticket_add_page))
        {
            Response.Redirect("LoginEN.aspx");
        }
    }

    protected void grdUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        
        grdUsers.DataSource = lstTicketGrid;
        grdUsers.PageIndex = e.NewPageIndex;
        grdUsers.DataBind();
    }


    /// <summary>
    /// this function will search in open tickets and get user open tickets
    /// </summary>
    /// <param name="txtText">the text to search for</param>
    /// <returns>a list of tickets found</returns>
    protected List<Ticket> search(string txtText)
    {
        string equality = "<>";
        if (bHistory.Value)
            equality = "=";

        TicketDB objTicketDB = new TicketDB(ConnectionStrings.LMS);
        List<Ticket> lstRet = new List<Ticket>();

        if (txtText.Trim() == "")
        {
            
            List<Ticket> lstNew = objTicketDB.Select(" top 100 ", " where [EmployeeID]='" + mobjPersons.GenID.ToString() + "' AND [StatusID]" + equality + "'2'  order by [ModifyTime] DESC ");
            //lstNew = new List<Persons>(from objFol in lstNew where objFol.StatusID != 2 select objFol);
            return lstNew;
        }


        string[] strValues = null;
        if (txtText.Trim()[0] == '\"' && txtText.Trim()[txtText.Trim().Length - 1] == '\"')
        {
            try
            {
                strValues = new string[] { txtText.Trim().Substring(1, txtText.Trim().Length - 2) };
                if (strValues[0] == "")
                    strValues = new string[0];
            }
            catch
            {
                strValues = new string[0];
            }
        }
        else
        {
            strValues = txtText.Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        }



        foreach (string strValue1 in strValues)
        {
            string strValue = strValue1;
            strValue = strValue.Replace("'", "''");
            strValue = "%" + strValue + "%";

            string strWhere = " where ([EmployeeID]='" + mobjPersons.GenID.ToString() + "' AND [StatusID]" + equality + "'2') AND ( " +
                " [Title] LIKE '" + strValue + "' OR " +
                " [Details] LIKE '" + strValue + "' OR " +
                " CONVERT(nvarchar,ID) LIKE '" + strValue + "'" +
                " )  order by [ModifyTime] DESC ";


            List<Ticket> lstTickets = objTicketDB.Select (" top 100 " , strWhere);

            //lstPersons = new List<Persons>(from objFol in lstPersons where objFol.StatusID != 2 select objFol);

            foreach (Ticket objFol in lstTickets)
            {
                if ((from objIn in lstRet where objIn.ID == objFol.ID select objIn).ToList().Count == 0)
                {
                    lstRet.Add(objFol);
                }
            }

        }


        return lstRet;



    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        bindGrid(txtSearch.Text);

    }
}